import requests
import json
import time

headers = {
    'Connection': 'keep-alive',
    'Pragma': 'no-cache',
    'Cache-Control': 'no-cache',
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'X-Requested-With': 'XMLHttpRequest',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36 Edg/84.0.522.40',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'Origin': 'https://xxcapp.xidian.edu.cn',
    'Sec-Fetch-Site': 'same-origin',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Dest': 'empty',
    'Referer': 'https://xxcapp.xidian.edu.cn/uc/wap/login',
    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
}



YQT_GYY = {
    'szgjcs': '',
    'szcs': '',
    'szgj': '',
    'zgfxdq': '0',
    'mjry': '0',
    'csmjry': '0',
    'tw': '2',
    'sfcxtz': '0',
    'sfjcbh': '0',
    'sfcxzysx': '0',
    'qksm': '',
    'sfyyjc': '0',
    'jcjgqr': '0',
    'remark': '',
    'address': '广东省广州市黄埔区九佛街道凤湖中路广州绿地城',
    'geo_api_info': '{"type":"complete","position":{"Q":23.321796875,"R":113.54531656901099,"lng":113.545317,"lat":23.321797},"location_type":"html5","message":"Get ipLocation failed.Get geolocation success.Convert Success.Get address success.","accuracy":126,"isConverted":true,"status":1,"addressComponent":{"citycode":"020","adcode":"440112","businessAreas":[],"neighborhoodType":"","neighborhood":"","building":"","buildingType":"","street":"峻岚街","streetNumber":"7号","country":"中国","province":"广东省","city":"广州市","district":"黄埔区","township":"九佛街道"},"formattedAddress":"广东省广州市黄埔区九佛街道凤湖中路广州绿地城","roads":[],"crosses":[],"pois":[],"info":"SUCCESS"}',
    'area': '广东省 广州市 黄埔区',
    'province': '广东省',
    'city': '广州市',
    'sfzx': '1',
    'sfjcwhry': '0',
    'sfjchbry': '0',
    'sfcyglq': '0',
    'gllx': '',
    'glksrq': '',
    'jcbhlx': '',
    'jcbhrq': '',
    'ismoved': '0',
    'bztcyy': '',
    'sftjhb': '0',
    'sftjwh': '0',
    'sfjcjwry': '0',
    'jcjg': ''
}
YQT_URL = 'https://xxcapp.xidian.edu.cn/ncov/wap/default/save',

CWJ_GYY = {
    'ymtys': '0',
    'sfzx': '1',
    'tw': '1',
    'area': '广东省 广州市 黄埔区',
    'city': '广州市',
    'province': '广东省',
    'address': '广东省广州市黄埔区九佛街道凤湖中路广州绿地城',
    'geo_api_info': '{"type":"complete","position":{"Q":23.321796875,"R":113.54531656901099,"lng":113.545317,"lat":23.321797},"location_type":"html5","message":"Get ipLocation failed.Get geolocation success.Convert Success.Get address success.","accuracy":126,"isConverted":true,"status":1,"addressComponent":{"citycode":"020","adcode":"440112","businessAreas":[],"neighborhoodType":"","neighborhood":"","building":"","buildingType":"","street":"峻岚街","streetNumber":"7号","country":"中国","province":"广东省","city":"广州市","district":"黄埔区","township":"九佛街道"},"formattedAddress":"广东省广州市黄埔区九佛街道凤湖中路广州绿地城","roads":[],"crosses":[],"pois":[],"info":"SUCCESS"}',
    'sfcyglq': '0',
    'sfyzz': '0',
    'qtqk': ''
}
CWJ_CHECK_URL = "https://xxcapp.xidian.edu.cn/xisuncov/wap/open-report/index"
CWJ_URL = "https://xxcapp.xidian.edu.cn/xisuncov/wap/open-report/save"


def login(session, username, password):
    data = {
        'username': username,
        'password': password
    }
    session.post('https://xxcapp.xidian.edu.cn/uc/wap/login/check',
                 headers=headers, data=data)


def submit(session, url, data):
    response = session.post(url, headers=headers, data=data)
    s = response.text
    j = json.loads(s)
    return j['m']


if __name__ == '__main__':
    users = [
        # ('username', 'password'),
    ]
    mode = "cwj"
    assert mode in ("yqt", "cwj")
    t = time.strftime("%Y:%m:%d %H:%M:%S", time.localtime(time.time()))
    for student_id, password in users:
        session = requests.session()
        login(session, student_id, password)
        if mode == "cwj":
            message = submit(session, CWJ_URL, CWJ_GYY)
        else:
            message = submit(session, YQT_URL, YQT_GYY)
        print(t, student_id, message)